package br.com.fsa.aplication.Util;

import java.util.HashMap;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import br.com.fsa.aplication.Exception.ConectionException;
import br.com.fsa.aplication.Model.MotoristaVO;
import br.com.fsa.aplication.Services.MotoristaService;

public class MotoristaConverter implements Converter
{

	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2)
	{
		try
		{
			if (arg2 != null && !arg2.trim().equals(""))
			{

				HashMap dto = new HashMap();
				dto.put("objeto", Long.parseLong(arg2));
				MotoristaService servico = new MotoristaService();
				return (MotoristaVO) servico.obterPorId(dto).get("resposta");

			}

		} catch (ConectionException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2)
	{
		if (arg2 instanceof MotoristaVO)
		{
			MotoristaVO motorista = (MotoristaVO) arg2;
			return String.valueOf(motorista.getMotoristaID());
		}
		return "";

	}

}